package com.sinping.qh.service.app.records.impl;

import com.google.common.collect.Lists;
import com.sinping.qh.api.exception.HandlerException;
import com.sinping.qh.api.support.ConstEum;
import com.sinping.qh.api.support.FrontPage;
import com.sinping.qh.api.support.ResDtoUtil;
import com.sinping.qh.domain.common.UploadImage;
import com.sinping.qh.domain.lab.LaboratoryDomain;
import com.sinping.qh.domain.records.CheckRecord;
import com.sinping.qh.domain.records.HiddendangerRecord;
import com.sinping.qh.domain.records.HighLightRecord;
import com.sinping.qh.domain.records.RectifyReviewDomain;
import com.sinping.qh.dto.app.records.HiddendangerByAppDTO;
import com.sinping.qh.dto.app.records.RectifyReviewByAppDTO;
import com.sinping.qh.dto.common.UploadImageDTO;
import com.sinping.qh.query.records.RectifyReviewQuery;
import com.sinping.qh.query.records.HiddenDangerQuery;
import com.sinping.qh.repository.mongo.RectifyReviewRepository;
import com.sinping.qh.repository.mongo.common.UploadImageRepository;
import com.sinping.qh.repository.mongo.lab.LaboratoryRepository;
import com.sinping.qh.repository.mongo.records.CheckRecordsRepository;
import com.sinping.qh.repository.mongo.records.HiddenDangerRecordRepository;
import com.sinping.qh.service.MongoManager;
import com.sinping.qh.service.app.records.IHiddenDangerService;
import com.sinping.qh.service.common.CommonDbConst;
import com.sinping.qh.utils.mapper.BeanMapper;
import com.sinping.qh.utils.misc.IdGenerator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author zhangjin
 * @create 2018/6/2
 */
@Service
public class HiddenDangerServiceImpl implements IHiddenDangerService {



    @Autowired
    MongoManager mongoManager;

    @Autowired
    HiddenDangerRecordRepository hiddenDangerRecordRepository;

    @Autowired
    CheckRecordsRepository checkRecordsRepository;

    @Autowired
    RectifyReviewRepository rectifyReviewRepository;


    @Autowired
    private UploadImageRepository imageRepository;

    /**
     *
     * @param hiddendangerByAppDTO
     * @return
     */
    @Autowired
    LaboratoryRepository laboratoryRepository;

    @Value(value = "${qh.upload.urlPath}")
    private String urlPath;

    @Override
    public HiddendangerByAppDTO save(HiddendangerByAppDTO hiddendangerByAppDTO) {

        HiddendangerRecord hiddendangerRecord = BeanMapper.map(hiddendangerByAppDTO,HiddendangerRecord.class);

        CheckRecord checkRecord=
                checkRecordsRepository.findOne(hiddendangerRecord.getCheckId());
        if(checkRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"找不到检查记录");
        }
        //检查状态不等于检查中
        if(!(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)))
        {
            throw new HandlerException(ConstEum.FAIL,"检查记录已提交审核或已经审核");
        }

        if(StringUtils.isEmpty(hiddendangerRecord.getDesc()) && StringUtils.isEmpty(hiddendangerRecord.getCheckItemThreeId()))
        {
            throw new HandlerException(ConstEum.FAIL,"隐患描述不能为空");
        }

        hiddendangerRecord.setId(IdGenerator.uuid2().toString());

        hiddendangerRecord.setHiddenDangerStatus(CommonDbConst.HIDDENDANGER_STATUS_READY_RECTIFY);

        if(StringUtils.isNotBlank(hiddendangerRecord.getCheckLabId())) {
            //查询所属所室
            LaboratoryDomain laboratoryDomain=laboratoryRepository.findOne(hiddendangerRecord.getCheckLabId().toString());
            if(laboratoryDomain!=null)
            {
                hiddendangerRecord.setOffices(laboratoryDomain.getOffices());
            }

        }
        //检查记录状态
        hiddendangerRecord.setCheckStatus(checkRecord.getCheckStatus());
        //检查记录日期
        hiddendangerRecord.setCheckDate(checkRecord.getCheckDate());

        //保存图片
        hiddendangerRecord.setUploadImageList(getUploadImage(hiddendangerByAppDTO.getUploadImageList()));

        hiddendangerRecord.setGmtCreated(new Date());
        hiddendangerRecord.setGmtModified(new Date());

        hiddendangerRecord= hiddenDangerRecordRepository.save(hiddendangerRecord);
        hiddendangerByAppDTO =BeanMapper.map(hiddendangerRecord, HiddendangerByAppDTO.class);
        return hiddendangerByAppDTO;
    }

    @Override
    public FrontPage findPage(FrontPage frontPage) {
        Sort sort;
        if(StringUtils.isNotBlank(frontPage.getProp()) &&
                StringUtils.isNotBlank(frontPage.getOrder()))
        {
            sort=new Sort(Sort.Direction.fromString(frontPage.getOrder()),frontPage.getProp());
        }else
        {
            sort = new Sort(Sort.Direction.DESC,"gmt_created");
        }
        //查询条件
        HiddenDangerQuery hiddenDangerQuery = BeanMapper.map(frontPage,HiddenDangerQuery.class);
        PageRequest page = new PageRequest(frontPage.getPage()-1,frontPage.getSize(),sort);


        Page<HiddendangerRecord> hiddendangerRecords =mongoManager.findPage(hiddenDangerQuery,page,HiddendangerRecord.class);

        for(HiddendangerRecord hiddendangerRecord:hiddendangerRecords) {
            //给图片设置路径
            if (hiddendangerRecord != null && hiddendangerRecord.getUploadImageList().size() > 0) {
                List<UploadImage> uploadImageDTOList =
                        hiddendangerRecord.getUploadImageList();

                for (UploadImage uploadImage:uploadImageDTOList) {
                    uploadImage.setPath(urlPath + uploadImage.getPath());
                }
            }
        }

        frontPage= ResDtoUtil.defaultUtil().pageDomainDtoToFrontDto(hiddendangerRecords,HiddendangerByAppDTO.class,frontPage);

        List<HiddendangerByAppDTO> hiddendangerByAppDTOS =frontPage.getRecords();

        for (HiddendangerByAppDTO hiddendangerByAppDTO:hiddendangerByAppDTOS ) {
            //增加整改复查
            List<RectifyReviewDomain> rectifyReviewDomainList=
                    rectifyReviewRepository.findAllByHiddenDangerIdOrderByGmtModifiedAsc(hiddendangerByAppDTO.getId());

            RectifyReviewByAppDTO reviewByAppDTO =new RectifyReviewByAppDTO();
            if(hiddendangerByAppDTO.getCaseGmtCreate()!=null){
                reviewByAppDTO.setId(hiddendangerByAppDTO.getId());
                reviewByAppDTO.setReType("3");
                reviewByAppDTO.setHiddenDangerStatus(hiddendangerByAppDTO.getHiddenDangerStatus());
                reviewByAppDTO.setDesc(hiddendangerByAppDTO.getCaseMethod());
                reviewByAppDTO.setSubmitTime(hiddendangerByAppDTO.getCaseGmtCreate());
                reviewByAppDTO.setPreTime(hiddendangerByAppDTO.getPreTime());
                reviewByAppDTO.setSubmitUserId(hiddendangerByAppDTO.getCaseUserNameId());
                reviewByAppDTO.setSubmitUserName(hiddendangerByAppDTO.getCaseUserName());
                reviewByAppDTO.setHiddenDangerId(hiddendangerByAppDTO.getId());
                reviewByAppDTO.setGmtCreated(hiddendangerByAppDTO.getCaseGmtCreate());
                reviewByAppDTO.setGmtModified(hiddendangerByAppDTO.getCaseGmtCreate());
            }
            List<RectifyReviewByAppDTO> hiddendangerByAppDTOList= Lists.newArrayList();
            for (RectifyReviewDomain rectifyReviewDomain :rectifyReviewDomainList) {

                //给图片设置路径
                if(rectifyReviewDomain!=null && rectifyReviewDomain.getUploadImageList().size()>0)
                {
                    List<UploadImage> uploadImageDTOListSecond=
                            rectifyReviewDomain.getUploadImageList();

                    for (UploadImage uploadImage:uploadImageDTOListSecond)
                    {
                        uploadImage.setPath(urlPath+uploadImage.getPath());
                    }
                }
                RectifyReviewByAppDTO rectifyReviewByAppDTO =
                        BeanMapper.map(rectifyReviewDomain,RectifyReviewByAppDTO.class);
                //是否要插入整改方案 app用到
                if(hiddendangerByAppDTO.getCaseGmtCreate()!=null
                        &&hiddendangerByAppDTO.getCaseGmtCreate().before(rectifyReviewByAppDTO.getGmtModified())){
                    if(!hiddendangerByAppDTOList.contains(reviewByAppDTO)) {
                        hiddendangerByAppDTOList.add(reviewByAppDTO);
                    }
                }
                hiddendangerByAppDTOList.add(rectifyReviewByAppDTO);
            }
            if(hiddendangerByAppDTO.getCaseGmtCreate()!=null&&
                    !hiddendangerByAppDTOList.contains(reviewByAppDTO))
            {
                hiddendangerByAppDTOList.add(reviewByAppDTO);
            }
            hiddendangerByAppDTO.setItems(hiddendangerByAppDTOList);
        }
        frontPage.setRecords(hiddendangerByAppDTOS);
        return frontPage;
    }

    @Override
    public HiddendangerByAppDTO findOne(String id) {
        HiddendangerRecord hiddendangerRecord =hiddenDangerRecordRepository.findOne(id);

        //给图片设置路径
        if(hiddendangerRecord!=null && hiddendangerRecord.getUploadImageList().size()>0)
        {
            List<UploadImage> uploadImageDTOList=
                    hiddendangerRecord.getUploadImageList();

            for (UploadImage uploadImage:uploadImageDTOList)
            {
                uploadImage.setPath(urlPath+uploadImage.getPath());
            }
        }

        HiddendangerByAppDTO hiddendangerByAppDTO =BeanMapper.map(hiddendangerRecord, HiddendangerByAppDTO.class);

        List<RectifyReviewDomain> rectifyReviewDomainList=
                rectifyReviewRepository.findAllByHiddenDangerIdOrderByGmtModifiedAsc(hiddendangerByAppDTO.getId());

        for(RectifyReviewDomain reviewByAppDTO:rectifyReviewDomainList)
        {
            //给图片设置路径
            if(reviewByAppDTO!=null && reviewByAppDTO.getUploadImageList().size()>0)
            {
                List<UploadImage> uploadImageDTOList=
                        reviewByAppDTO.getUploadImageList();

                for (UploadImage uploadImage:uploadImageDTOList)
                {
                    uploadImage.setPath(urlPath+uploadImage.getPath());
                }
            }
        }

        RectifyReviewByAppDTO reviewByAppDTO =new RectifyReviewByAppDTO();
        if(hiddendangerByAppDTO.getCaseGmtCreate()!=null){
            reviewByAppDTO.setId(hiddendangerByAppDTO.getId());
            reviewByAppDTO.setReType("3");
            reviewByAppDTO.setHiddenDangerStatus(hiddendangerByAppDTO.getHiddenDangerStatus());
            reviewByAppDTO.setDesc(hiddendangerByAppDTO.getCaseMethod());
            reviewByAppDTO.setSubmitTime(hiddendangerByAppDTO.getCaseGmtCreate());
            reviewByAppDTO.setPreTime(hiddendangerByAppDTO.getPreTime());
            reviewByAppDTO.setSubmitUserId(hiddendangerByAppDTO.getCaseUserNameId());
            reviewByAppDTO.setSubmitUserName(hiddendangerByAppDTO.getCaseUserName());
            reviewByAppDTO.setHiddenDangerId(hiddendangerByAppDTO.getId());
            reviewByAppDTO.setGmtCreated(hiddendangerByAppDTO.getCaseGmtCreate());
            reviewByAppDTO.setGmtModified(hiddendangerByAppDTO.getCaseGmtCreate());
        }

        List<RectifyReviewByAppDTO> hiddendangerByAppDTOList= Lists.newArrayList();
        for (RectifyReviewDomain rectifyReviewDomain :rectifyReviewDomainList) {

            RectifyReviewByAppDTO rectifyReviewByAppDTO =
                    BeanMapper.map(rectifyReviewDomain,RectifyReviewByAppDTO.class);
//            rectifyReviewByAppDTO.setHiddenDangerStatus(hiddendangerByAppDTO.getHiddenDangerStatus());
            //是否要插入整改方案 app用到
            if(hiddendangerByAppDTO.getCaseGmtCreate()!=null
                    &&hiddendangerByAppDTO.getCaseGmtCreate().before(rectifyReviewByAppDTO.getGmtModified())){
                if(!hiddendangerByAppDTOList.contains(reviewByAppDTO)) {
                    hiddendangerByAppDTOList.add(reviewByAppDTO);
                }
            }
            hiddendangerByAppDTOList.add(rectifyReviewByAppDTO);


        }
        if(hiddendangerByAppDTO.getCaseGmtCreate()!=null&&
                !hiddendangerByAppDTOList.contains(reviewByAppDTO))
        {
            hiddendangerByAppDTOList.add(reviewByAppDTO);
        }
        hiddendangerByAppDTO.setItems(hiddendangerByAppDTOList);

        return hiddendangerByAppDTO;
    }

    /**
     * 一、删除隐患和亮点： 条件：检查记录状态为检查中   物理删除
     * 二、如果是检查组长，审核时也可以删除隐患和亮点
     * @param id
     */
    @Override
    public boolean delete(String id) {

        HiddendangerRecord hiddendangerRecord=hiddenDangerRecordRepository.findOne(id);
        CheckRecord checkRecord=checkRecordsRepository.findOne(hiddendangerRecord.getCheckId());
        if(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)) {
            hiddenDangerRecordRepository.delete(id);
        }
        else
        {
            throw new HandlerException(ConstEum.FAIL,"检查记录待审核或已审核");
        }

        return true;
    }

    @Override
    public HiddendangerByAppDTO update(HiddendangerByAppDTO hiddendangerByAppDTO) {
        if(StringUtils.isEmpty(hiddendangerByAppDTO.getId()))
        {
            throw new HandlerException(ConstEum.FAIL,"隐患Id不能为空");
        }
        HiddendangerRecord hiddendangerRecord = hiddenDangerRecordRepository.findOne(hiddendangerByAppDTO.getId());
        if(hiddendangerRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"错误的隐患Id");
        }
        CheckRecord checkRecord=
                checkRecordsRepository.findOne(hiddendangerRecord.getCheckId());
        if(checkRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"找不到检查记录");
        }
        //检查状态不等于检查中
        if(!(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)))
        {
            throw new HandlerException(ConstEum.FAIL,"检查记录已提交审核或已经审核");
        }
        if(StringUtils.isEmpty(hiddendangerByAppDTO.getDesc()) && StringUtils.isEmpty(hiddendangerByAppDTO.getCheckItemThreeId()))
        {
            throw new HandlerException(ConstEum.FAIL,"隐患描述不能为空");
        }
        //检查项一级
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getCheckItemOne()))
//        {
            hiddendangerRecord.setCheckItemOne(hiddendangerByAppDTO.getCheckItemOne());
//        }
        //检查项一级id
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getCheckItemOneId()))
//        {
            hiddendangerRecord.setCheckItemOneId(hiddendangerByAppDTO.getCheckItemOneId());
//        }
        //检查项二级
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getCheckItemTwo()))
//        {
            hiddendangerRecord.setCheckItemTwo(hiddendangerByAppDTO.getCheckItemTwo());
//        }
        //检查项二级id
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getCheckItemTwoId()))
//        {
            hiddendangerRecord.setCheckItemTwoId(hiddendangerByAppDTO.getCheckItemTwoId());
//        }
        //desc
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getDesc()))
//        {
            hiddendangerRecord.setDesc(hiddendangerByAppDTO.getDesc());
//        }
        //检查项三级
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getCheckItemThree()))
//        {
            hiddendangerRecord.setCheckItemThree(hiddendangerByAppDTO.getCheckItemThree());
//        }
        //检查项三级id
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getCheckItemThreeId()))
//        {
            hiddendangerRecord.setCheckItemThreeId(hiddendangerByAppDTO.getCheckItemThreeId());
//        }
        //hidden_danger_status
        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getHiddenDangerStatus()))
        {
            hiddendangerRecord.setHiddenDangerStatus(hiddendangerByAppDTO.getHiddenDangerStatus());
        }

//        if(hiddendangerByAppDTO.getUploadImageList()!=null&&hiddendangerByAppDTO.getUploadImageList().size()>0) {
            //保存图片
            hiddendangerRecord.setUploadImageList(getUploadImage(hiddendangerByAppDTO.getUploadImageList()));
//        }

        hiddendangerRecord.setGmtModified(new Date());

        hiddendangerRecord=hiddenDangerRecordRepository.save(hiddendangerRecord);

        hiddendangerByAppDTO =BeanMapper.map(hiddendangerRecord,HiddendangerByAppDTO.class);
        return hiddendangerByAppDTO;
    }

    @Override
    public boolean hiddenDangerRectify(RectifyReviewByAppDTO rectifyReviewByAppDTO) {

        HiddendangerRecord hiddendangerRecord =hiddenDangerRecordRepository.findOne(rectifyReviewByAppDTO.getHiddenDangerId());
        if(hiddendangerRecord==null)
        {
            throw new HandlerException(ConstEum.FAIL,"未找到隐患记录");
        }
        //整改后为待复查
        hiddendangerRecord.setHiddenDangerStatus(CommonDbConst.HIDDENDANGER_STATUS_READY_REVIEW);
        hiddendangerRecord.setGmtModified(new Date());


        hiddenDangerRecordRepository.save(hiddendangerRecord);


        RectifyReviewDomain rectifyReviewDomain =BeanMapper.map(rectifyReviewByAppDTO,RectifyReviewDomain.class);
        rectifyReviewDomain.setId(IdGenerator.uuid2().toString());
        //保存图片
        rectifyReviewDomain.setUploadImageList(getUploadImage(rectifyReviewByAppDTO.getUploadImageList()));
        rectifyReviewDomain.setDesc(rectifyReviewByAppDTO.getDesc());
        rectifyReviewDomain.setReType(CommonDbConst.DB_HIDDENDANGER_RECTIFY);
        //提交者id
        rectifyReviewDomain.setSubmitUserId(rectifyReviewByAppDTO.getSubmitUserId());
        //提交者姓名
        rectifyReviewDomain.setSubmitUserName(rectifyReviewByAppDTO.getSubmitUserName());

//        rectifyReviewDomain.setHiddenDangerStatus(CommonDbConst.HIDDENDANGER_STATUS_READY_REVIEW);

        rectifyReviewDomain.setSubmitTime(new Date());
        rectifyReviewDomain.setGmtCreated(new Date());
        rectifyReviewDomain.setGmtModified(new Date());
        rectifyReviewRepository.save(rectifyReviewDomain);
        return true;

    }

    @Override
    public boolean hiddenDangerReview(RectifyReviewByAppDTO rectifyReviewByAppDTO) {

        HiddendangerRecord hiddendangerRecord =hiddenDangerRecordRepository.findOne(rectifyReviewByAppDTO.getHiddenDangerId());
        //整改后为待复查
        hiddendangerRecord.setHiddenDangerStatus(rectifyReviewByAppDTO.getHiddenDangerStatus());
        hiddendangerRecord.setGmtModified(new Date());
        hiddendangerRecord=hiddenDangerRecordRepository.save(hiddendangerRecord);
        if(hiddendangerRecord!=null)
        {
            RectifyReviewDomain rectifyReviewDomain =BeanMapper.map(rectifyReviewByAppDTO,RectifyReviewDomain.class);
            rectifyReviewDomain.setId(IdGenerator.uuid2().toString());
            //保存图片
            rectifyReviewDomain.setUploadImageList(getUploadImage(rectifyReviewByAppDTO.getUploadImageList()));
            rectifyReviewDomain.setReType(CommonDbConst.DB_HIDDENDANGER_REVIEW);
            rectifyReviewDomain.setHiddenDangerStatus(rectifyReviewByAppDTO.getHiddenDangerStatus());
            rectifyReviewDomain.setSubmitTime(new Date());
            rectifyReviewDomain.setGmtCreated(new Date());
            rectifyReviewDomain.setGmtModified(new Date());
            rectifyReviewRepository.save(rectifyReviewDomain);
            return true;
        }
        else
        {
            return false;
        }
    }

    @Override
    public FrontPage rectifyRecviewByHDId(FrontPage frontPage) {
        Sort sort;
        if(StringUtils.isNotBlank(frontPage.getProp()) &&
                StringUtils.isNotBlank(frontPage.getOrder()))
        {
            sort=new Sort(frontPage.getOrder(),frontPage.getProp());
        }else
        {
            sort = new Sort(Sort.Direction.DESC,"gmt_created");
        }
        //查询条件
        RectifyReviewQuery rectifyReviewQuery = BeanMapper.map(frontPage,RectifyReviewQuery.class);


        PageRequest page = new PageRequest(frontPage.getPage()-1,frontPage.getSize(),sort);


        Page<RectifyReviewDomain> rectifyReviewDomains =mongoManager.findPage(rectifyReviewQuery,page,RectifyReviewDomain.class);


        frontPage= ResDtoUtil.defaultUtil().pageDomainDtoToFrontDto(rectifyReviewDomains,RectifyReviewByAppDTO.class,frontPage);
        return frontPage;
    }

//    @Override
//    public void updateHiddenDangerByOffLine(HiddendangerByAppDTO hiddendangerByAppDTO)
//    {
//        CheckRecord checkRecord=
//                checkRecordsRepository.findOne(hiddendangerByAppDTO.getCheckId());
//        if(checkRecord==null)
//        {
//            throw new HandlerException(ConstEum.FAIL,"找不到检查记录");
//        }
//        //检查状态不等于检查中
//        if(!(checkRecord.getCheckStatus().equals(CommonDbConst.CHECKRECORD_STATUS_CHECKING)))
//        {
//            throw new HandlerException(ConstEum.FAIL,"检查记录已提交审核或已经审核");
//        }
//
//        if(StringUtils.isEmpty(hiddendangerByAppDTO.getDesc()) && StringUtils.isEmpty(hiddendangerByAppDTO.getCheckItemThreeId()))
//        {
//            throw new HandlerException(ConstEum.FAIL,"隐患描述不能为空");
//        }
//
//        if(StringUtils.isNotBlank(hiddendangerByAppDTO.getId())){
//
//            HiddendangerRecord hiddendangerRecord = hiddenDangerRecordRepository.findOne(hiddendangerByAppDTO.getId());
//            if(hiddendangerRecord==null)
//            {
//                throw new HandlerException(ConstEum.FAIL,"错误的隐患Id");
//            }
//            //检查项一级
//            hiddendangerRecord.setCheckItemOne(hiddendangerByAppDTO.getCheckItemOne());
//            //检查项一级id
//            hiddendangerRecord.setCheckItemOneId(hiddendangerByAppDTO.getCheckItemOneId());
//            //检查项二级
//            hiddendangerRecord.setCheckItemTwo(hiddendangerByAppDTO.getCheckItemTwo());
//            //检查项二级id
//            hiddendangerRecord.setCheckItemTwoId(hiddendangerByAppDTO.getCheckItemTwoId());
//            //desc
//            hiddendangerRecord.setDesc(hiddendangerByAppDTO.getDesc());
//            //检查项三级
//            hiddendangerRecord.setCheckItemThree(hiddendangerByAppDTO.getCheckItemThree());
//            //检查项三级id
//            hiddendangerRecord.setCheckItemThreeId(hiddendangerByAppDTO.getCheckItemThreeId());
//            //hidden_danger_status
//            if(StringUtils.isNotBlank(hiddendangerByAppDTO.getHiddenDangerStatus()))
//            {
//                hiddendangerRecord.setHiddenDangerStatus(hiddendangerByAppDTO.getHiddenDangerStatus());
//            }
//
////        if(hiddendangerByAppDTO.getUploadImageList()!=null&&hiddendangerByAppDTO.getUploadImageList().size()>0) {
//            //保存图片
//            hiddendangerRecord.setUploadImageList(getUploadImage(hiddendangerByAppDTO.getUploadImageList()));
////        }
//
//            hiddendangerRecord.setGmtModified(new Date());
//
//            HiddendangerRecord newHiddendangerRecord = hiddenDangerRecordRepository.save(hiddendangerRecord);
//            if(newHiddendangerRecord ==null){
//                throw new HandlerException(ConstEum.FAIL,ConstEum.FAIL.getMessage());
//            }
//        }else {
//            HiddendangerRecord hiddendangerRecord = BeanMapper.map(hiddendangerByAppDTO,HiddendangerRecord.class);
//            hiddendangerRecord.setId(IdGenerator.uuid2().toString());
//
//            hiddendangerRecord.setHiddenDangerStatus(CommonDbConst.HIDDENDANGER_STATUS_READY_RECTIFY);
//
//            if (StringUtils.isNotBlank(hiddendangerRecord.getCheckLabId())) {
//                //查询所属所室
//                LaboratoryDomain laboratoryDomain = laboratoryRepository.findOne(hiddendangerRecord.getCheckLabId().toString());
//                if (laboratoryDomain != null) {
//                    hiddendangerRecord.setOffices(laboratoryDomain.getOffices());
//                }
//
//            }
//            //检查记录状态
//            hiddendangerRecord.setCheckStatus(checkRecord.getCheckStatus());
//            //检查记录日期
//            hiddendangerRecord.setCheckDate(checkRecord.getCheckDate());
//
//            //保存图片
//            hiddendangerRecord.setUploadImageList(getUploadImage(hiddendangerByAppDTO.getUploadImageList()));
//
//            hiddendangerRecord.setGmtCreated(new Date());
//            hiddendangerRecord.setGmtModified(new Date());
//
//            HiddendangerRecord newHiddendangerRecord = hiddenDangerRecordRepository.save(hiddendangerRecord);
//            if(newHiddendangerRecord ==null){
//                throw new HandlerException(ConstEum.FAIL,ConstEum.FAIL.getMessage());
//            }
//
//        }
//
//    }


    private List<UploadImage> getUploadImage(List<UploadImageDTO> arrayList)
    {
        ArrayList<UploadImage> uploadImages = new ArrayList<UploadImage>();
        for(UploadImageDTO uploadImageDTO :arrayList) {
            if(uploadImageDTO!=null && StringUtils.isNotBlank(uploadImageDTO.getId())) {
                UploadImage uploadImage = imageRepository.findOne(uploadImageDTO.getId());
                if(uploadImage==null)
                {
                    throw new HandlerException(ConstEum.FAIL,"无效的图片id");
                }
                uploadImages.add(uploadImage);
            }
        }
        return uploadImages;

    }
}
